#ifndef  __EXTEN_16CH_ADC_H__
#define  __EXTEN_16CH_ADC_H__

#include <avr/io.h>


#define		MY_SPI_DDR	DDRA
#define		SPI_OUT 	PORTA
#define		SPI_IN  	PORTA

#define  CS_PIN 	PA3
#define  CLK_PIN 	PA1
#define  MOSI_PIN	PA2
#define  MISO_PIN	PA0

#define CHIP_SELECT 	SPI_OUT&=~(1<<CS_PIN)
#define CHIP_DESELECT 	SPI_OUT|=1<<CS_PIN
#define CLK1			SPI_OUT|=1<<CLK_PIN
#define CLK0			SPI_OUT&=~(1<<CLK_PIN)

#define MOSI1			SPI_OUT|=1<<MOSI_PIN
#define MOSI0			SPI_OUT&=~(1<<MOSI_PIN)

#define MISO			SPI_IN&(1<<MISO_PIN)

#define WRITE 		11
#define SEQ 		10
#define ADD3		9
#define ADD2		8
#define ADD1		7
#define ADD0		6
#define PM1			5
#define PM0			4
#define SHADOWN		3
#define WEAK		2
#define RANGE		1
#define CODING		0


#define EXT_ADC0		(0<<ADD3|0<<ADD2|0<<ADD1|0<<ADD0)<<4
#define EXT_ADC1		(0<<ADD3|0<<ADD2|0<<ADD1|1<<ADD0)<<4
#define EXT_ADC2		(0<<ADD3|0<<ADD2|1<<ADD1|0<<ADD0)<<4
#define EXT_ADC3		(0<<ADD3|0<<ADD2|1<<ADD1|1<<ADD0)<<4
#define EXT_ADC4		(0<<ADD3|1<<ADD2|0<<ADD1|0<<ADD0)<<4
#define EXT_ADC5		(0<<ADD3|1<<ADD2|0<<ADD1|1<<ADD0)<<4
#define EXT_ADC6		(0<<ADD3|1<<ADD2|1<<ADD1|0<<ADD0)<<4
#define EXT_ADC7		(0<<ADD3|1<<ADD2|1<<ADD1|1<<ADD0)<<4
#define EXT_ADC8		(1<<ADD3|0<<ADD2|0<<ADD1|0<<ADD0)<<4
#define EXT_ADC9		(1<<ADD3|0<<ADD2|0<<ADD1|1<<ADD0)<<4
#define EXT_ADC10		(1<<ADD3|0<<ADD2|1<<ADD1|0<<ADD0)<<4
#define EXT_ADC11		(1<<ADD3|0<<ADD2|1<<ADD1|1<<ADD0)<<4
#define EXT_ADC12		(1<<ADD3|1<<ADD2|0<<ADD1|0<<ADD0)<<4
#define EXT_ADC13		(1<<ADD3|1<<ADD2|0<<ADD1|1<<ADD0)<<4
#define EXT_ADC14		(1<<ADD3|1<<ADD2|1<<ADD1|0<<ADD0)<<4
#define EXT_ADC15		(1<<ADD3|1<<ADD2|1<<ADD1|1<<ADD0)<<4


#define NORMAL_MODE 		(1<<PM1|1<<PM0)<<4
#define NORMAL_RANGE 		(1<<RANGE)<<4
#define	ONE_CONV			(0<<SEQ|0<<SHADOWN)<<4
#define WRITE_CONTROL_REG	(1<<WRITE)<<4
#define WEAK_CONF			(1<<WEAK)<<4

#define CONF_ADC			WRITE_CONTROL_REG|NORMAL_MODE|NORMAL_RANGE|ONE_CONV|WEAK_CONF|(1<<CODING)<<4






void		   Init_16ch_adc(void);
void 		   write_contrl_registr(unsigned short byte);
unsigned short write_shadow_registr(unsigned short byte);
unsigned short read_registr_data(unsigned short byte);
unsigned short ext_read_adc(unsigned short adc_mux);

#endif
